<?php
defined('IN_MAIN') or exit('Invalid Access!');
    
function build_matrix_network($hid, $enable_radio, $highlight_uid="", $selected_slot="", $css_prefix="admin"){
/*
This function build the matrix network for the input hid (head id), with the matrix head being the hid.
if enable radio is true, then it will output the empty box (box without downline user) with a radio button.
if input row is not zero, then it will start to display the matrix network from the input "row", and this also depends on the present of the
querystring "row", if querystring "row" present, then it will display that particular row according to the querystring (highest priority).
the highlight_uid is it will highlight the slot if that slot user id is same with this highlight_uid. if it is the 1st time of building
this matrix network, and highlight_uid is a direct matrix downline of hid, then it will search for the highlight_uid and display the matrix
network in such that the highlight_uid is visible on the 1st time build, and will navigate to appropriate row for that.
*/

global $vars, $get_s;
$db=$vars["db"];

$total_slot=2;

//verification
$head_info=get_user_detail_by_id($hid);
if(!$head_info){
 $break=true;
}
if($get_s["cur_uid"] && $get_s["cur_uid"]!=$hid){
 $head_info=get_user_detail_by_id($get_s["cur_uid"]);
 if(!$head_info){
  $break=true;
 }elseif(!is_group_matrix_downline_of($get_s["cur_uid"], $hid)){
  $break=true;
 }
}
if($break){
 return;
}

//get current head id
if($get_s["cur_uid"]){
 $head_id=$get_s["cur_uid"];
}else{
 $head_id=$hid;
}

//determine starting row
$starting_row=1;

$position_start=(($starting_row-1) * $total_slot) + 1;
$position_end=$position_start + $total_slot - 1;
//retrieve downlines within that row
$t=@mysql_num_rows($r=mysql_query($sql="select * from $db->users where matrix_upline='$head_id' and matrix_position>=$position_start and matrix_position<=$position_end"));
for($i=0;$i<$t;$i++){
 $downline=mysql_fetch_assoc($r);
 $downlines[$downline["matrix_position"]]=$downline;
 $downlines[$downline["matrix_position"]]["total_downline"]=get_total_group_downline($downline['id']);
 //retrieve level 2
 $t2=@mysql_num_rows($r2=mysql_query($sql="select * from $db->users where matrix_upline='$downline[id]'"));
 for($j=0;$j<$t2;$j++){
  $downline_l2=mysql_fetch_assoc($r2);
  $downlines_lvl2[$downline['matrix_position']][$downline_l2['matrix_position']]=$downline_l2;
  $downlines_lvl2[$downline['matrix_position']][$downline_l2['matrix_position']]['total_downline']=get_total_group_downline($downline_l2['id']);
 }
}
//build the slots
$cur_uid_qs=preg_replace('/\&*cur_uid=[0-9]*/', '', $_SERVER["QUERY_STRING"]);
$cur_uid_qs=preg_replace('/\&*row=[0-9]*/', '', $cur_uid_qs);
for($i=0;$i<$total_slot;$i++){
 $this_slot_index = (($starting_row-1) * $total_slot) + ($i+1);
 $slots.=
   "<td colspan='2' width='50%'>
     <div class='downline_slot'>
      <div class='slot_box".($highlight_uid && $downlines[$this_slot_index]["id"]==$highlight_uid? " slot_highlight" : "")."'>
      <div class='slot_number'>".($i==0? __("Left") : __("Right"))."</div>".(isset($downlines[$this_slot_index])?
       "ID: ".$downlines[$this_slot_index]["id"]."<br />\n".$downlines[$this_slot_index]["name"] :
       ($enable_radio? "<br />\n<input type='radio' name='slot' ".($selected_slot && $selected_slot=="$head_id-$this_slot_index"? "checked='checked'" : "")." value='$head_id-$this_slot_index' />" : "")).
       (isset($downlines[$this_slot_index])? "<br />\n<br />\n<span class='small_text'><a href='$vars[this_file]?$cur_uid_qs&cur_uid=".$downlines[$this_slot_index]["id"]."'>".replace_tag(__("<%x%> <%downline%>"), array("<%x%>"=>strval($downlines[$this_slot_index]["total_downline"]), "<%downline%>"=>$downlines[$this_slot_index]["total_downline"]>1? __("downlines") : __("downline")))."</a></span>" : "")."
      </div>
     </div>
     <div class='connector-1'></div>
     <div class='connector-2'></div>
   </td>";
}
$downline_slots.="
  <tr>
    $slots
  </tr>";
$slots='';

//build 2nd level slots
for($i=0;$i<$total_slot;$i++){
 for($j=0;$j<$total_slot;$j++){
  $lvl1_slot_index = $i+1;
  $this_slot_index = $j+1;
  $slots.=
   "<td width='25%'>
     <div class='downline_slot'>
       <div class='slot_box".($highlight_uid && $downlines_lvl2[$lvl1_slot_index][$this_slot_index]["id"]==$highlight_uid? " slot_highlight" : "")."'>
       <div class='slot_number'>".($this_slot_index==1? __("Left") : __("Right"))."</div>".(isset($downlines_lvl2[$lvl1_slot_index][$this_slot_index])?
       "ID: ".$downlines_lvl2[$lvl1_slot_index][$this_slot_index]["id"]."<br />\n".$downlines_lvl2[$lvl1_slot_index][$this_slot_index]["name"] :
       (isset($downlines[$lvl1_slot_index]) && $enable_radio? "<br />\n<input type='radio' name='slot' ".($selected_slot && $selected_slot==$downlines[$lvl1_slot_index]['id']."-$this_slot_index"? "checked='checked'" : "")." value='".$downlines[$lvl1_slot_index]['id']."-$this_slot_index' />" : "")).
       (isset($downlines_lvl2[$lvl1_slot_index][$this_slot_index])? "<br />\n<br />\n<span class='small_text'><a href='$vars[this_file]?$cur_uid_qs&cur_uid=".$downlines_lvl2[$lvl1_slot_index][$this_slot_index]["id"]."'>".replace_tag(__("<%x%> <%downline%>"), array("<%x%>"=>strval($downlines_lvl2[$lvl1_slot_index][$this_slot_index]["total_downline"]), "<%downline%>"=>$downlines_lvl2[$lvl1_slot_index][$this_slot_index]["total_downline"]>1? __("downlines") : __("downline")))."</a></span>" : "")."
       </div>
     </div>
   </td>";
 }
}
$downline_slots.="
  <tr>
    $slots
  </tr>";
$slots='';

//head slot
$head_total_downline=get_total_group_downline($head_id);
$cur_uid_qs=preg_replace('/\&*cur_uid=[0-9]*/', '', $_SERVER["QUERY_STRING"]);
$cur_uid_qs=preg_replace('/\&*row=[0-9]*/', '', $cur_uid_qs);
$head_slot=
  "<tr>
    <td colspan='4'>
      <div class='center'>".($get_s["cur_uid"] && $get_s["cur_uid"]!=$hid? "<a href='$vars[this_file]?$cur_uid_qs&cur_uid=$head_info[matrix_upline]'>Up</a>" : "")."</div>
      <div class='head_slot'>
       <div class='slot_box".($highlight_uid && $head_info["id"]==$highlight_uid? " slot_highlight" : "")."'>
        ID: $head_info[id]<br />\n
        $head_info[name]<br />\n<br />\n
        <span class='small_text'>".replace_tag(__("<%x%> <%downline%>"), array("<%x%>"=>strval($head_total_downline), "<%downline%>"=>$head_total_downline>1? __("downlines") : __("downline")))."</span>
       </div>
      </div>
      <div class='connector-1'></div>
      <div class='connector-2'></div>
    </td>
  </tr>";

//whole matrix
$matrix="
<input type='hidden' name='upline' value=\"$head_id\" />
<div class='".$css_prefix."_matrix'>
 <table class='matrix-body'>
  $head_slot
  $downline_slots
 </table>
</div>";

return $matrix;
}
?>